/*
    Copyright 2010 Yurtaev Egor

    This file is part of webapp-dcpp-log.

    webapp-dcpp-log is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    webapp-dcpp-log is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with webapp-dcpp-log. If not, see http://www.gnu.org/licenses/.
 */
var admins = new Array ("Andry74", "Herurg", "iddqd", "Pilot5575", "Хранитель", "Jesprit", "0CTPOB");

function linsear(v,t)
{
    var k=false
    for (var i=0; i <= v.length-1; i++)
        if (v[i] == t )
        {
            k=true;
            break
        }
    return k
}

function getData(url, func){
    request.open("GET", url, true);
    request.onreadystatechange = func;
    request.send(null);
}

function updateMsg(){
    if (request.readyState == 4) {
        if (request.status == 200) {
            var jsonData = eval('(' + request.responseText + ')');
            if (jsonData.data[0] == null) {
            }
            else {
                var color_nick = "";
                for (i=0; i<jsonData.data.length; i++) {
                    var list = document.getElementById('update')                   
                    var container = document.createElement('div');
                    container.id = jsonData.data[i].rowid;
                    container.className = "messages";
                    if (linsear(admins, jsonData.data[i].nick)) //проверяем ник в списке ников админов
                    {
                        color_nick = "msg_user_admin"; //если ник принадлежит администраторам то используем класс css msg_user_admin
                    } else {
                        color_nick = "msg_user"; //иначе msg_user
                    }
                    container.innerHTML = '<div style="visibility: visible; opacity: 1;" class="msg"><div class="msg_time">' + jsonData.data[i].time + '</div>\
                			    <div class="' + color_nick + '"><b>&#60;' + jsonData.data[i].nick + '&#62;</b></div>\
                			    <div class="msg_text">' + jsonData.data[i].message + '</div></div>';
                    list.appendChild(container); //добавляем блок нового сообщения
                }
                lastId = list.getElementsByClassName('messages')[list.getElementsByClassName('messages').length - 1].id;
                var list1 = document.getElementById('update')
                list1.scrollTop = list1.scrollHeight; //скролим в самый низ при появление нового сообщения
            }
        }
    }
}

function updateUserlist(){
    if (request.readyState == 4) {
        if (request.status == 200) {
            var jsonData = eval('(' + request.responseText + ')');
            if (jsonData.data[0] == null) {
                console.info("null data");
            }
            else {
                    for (i=0; i<jsonData.data.length; i++) {
                        var list = document.getElementById('userlist')
                        var container = document.createElement('option');
                        container.innerHTML = jsonData.data[i];
                        list.appendChild(container);
                }
            }
        }
    }
}

var lastId = null;

function first_run() {
    setInterval(function(){
        getData("/json/"+lastId+"/",updateMsg)
    }, 3000);
    getData("/userlist/", updateUserlist)
}

function IDLastMsg(){
    if (request.readyState == 4) {
        if (request.status == 200) {
            var jsonData = eval('(' + request.responseText + ')');
            if (jsonData.data[0] == null) {
            console.info("null data");
            }
            else {
                lastId = jsonData.data[0]-25;
                first_run()
            }
        }
    }
}